﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.IO;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using Git;
using DevExpress.XtraEditors;
using GitFrom;

namespace GitConfig
{
    public partial class GitConfig : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public GitConfig()
        {
            InitializeComponent();
            git.LoadToTree(this.treeList1);
            this.treeList1.DataSource = git.fetchcodelist;
            if (git.gitexelist != null && git.gitexelist.Count > 0)
            {
                this.textEdit_giturl.Text = git.gitexelist.Find(x => x.Value == "GITURL").Key;
                this.buttonEdit_gitexe.Text = git.gitexelist.Find(x=>x.Value == "GITEXE").Key;
            }

        }

        private TreeListNode tn = null;


        /// <summary>
        /// 获取当前节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static TreeListNode getFocusNode(Object sender, Point p)
        {
            TreeList currentTreeList = sender as TreeList;
            TreeListHitInfo hitinfo = currentTreeList.CalcHitInfo(p);
            if (hitinfo.Node != null)
            {
                return hitinfo.Node;
            }
            return null;
        }


        /// <summary>
        /// 同步分支
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_同步分支_Click(object sender, EventArgs e)
        {
            git g = new git();
            /*gitconfig gcs = git.gitexelist.Find(x => x.Value == "GITProductURL");
            if (gcs != null)
            {
                if (gcs.Key != null) {
                    if (Directory.Exists(gcs.Key))
                    {
                        g.gitbranch(gcs.Key);
                        this.treeList1.DataSource = git.fetchcodelist;
                        this.treeList1.RefreshDataSource();
                        FromBranch fb = new FromBranch();
                        fb.branch(null);
                        git.Save();
                        return;
                    }
                } 
            }*/
            gitconfig gc = git.gitexelist.Find(x => x.Value == "GITEXE");
            git.gitexelist.Clear();
            if (gc != null) {
                if (gc.Key != null && gc.Key.Length > 0)
                {
                    
                    Boolean b = g.gitclone(gc.Key, this.textEdit_giturl.Text);
                    if (b)
                    {
                        git.gitexelist.Add(gc);
                        git.gitexelist.Add(new gitconfig
                        {
                            Key = this.textEdit_giturl.Text,
                            Value = "GITURL",
                            Branch = "",
                            ID = System.Guid.NewGuid().ToString(),
                            ParentID = ""
                        });

                        gc = git.gitexelist.Find(x => x.Value == "GITProductURL");
                        if (gc != null)
                        {
                            if (gc.Key != null)
                            {
                                if (Directory.Exists(gc.Key))
                                {
                                    g.gitbranch(gc.Key);
                                    this.treeList1.DataSource = git.fetchcodelist;
                                    this.treeList1.RefreshDataSource();
                                    FromBranch fb = new FromBranch();
                                    fb.branch();
                                    git.Save();
                                    return;
                                }
                            }
                        }
                        git.Save();
                        XtraMessageBox.Show("成功", "成功", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    }
                    else {
                        XtraMessageBox.Show("失败", "失败", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    }
                }
                
            }
        }

        /// <summary>
        /// git标签 小按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.buttonEdit_gitexe.Text = dialog.FileName;
                git git = new git();
                Boolean b = git.gittest(dialog.FileName);
                if (b)
                {
                    git.gitexelist.Add(new gitconfig
                    {
                        Key = dialog.FileName,
                        Value = "GITEXE",
                        Branch = "",
                        ID = System.Guid.NewGuid().ToString(),
                        ParentID = ""
                    });
                    XtraMessageBox.Show("成功", "成功", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                }
                else { 
                    XtraMessageBox.Show("失败", "失败", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                }
            }
        }


        


    }


}